<?php
$GLOBALS['Md'] = "Support";
$GLOBALS['Pd'] = "B";
require_once($_SERVER['DOCUMENT_ROOT']."/defaultconf.php");
print heade(); 
print body(); 
module($BSD['DB00']);
office_header($_SESSION['office']);
include($GLOBALS['PAD']."Modules/Support/main.obj.php");
include($GLOBALS['PAD']."Modules/Support/nav.views.php");
print NavBack();
// SAVE TICKET
if (isset($_POST['SaveTicket'])) {
	if ($_POST['Area'] != "") {
	$timeCreation = date("Y-m-d H:i:s");
	 if ($_POST['date_occur'] != "AAAAMMDD") {

	$dateocAno = substr($_POST['date_occur'],0,4);
	$dateocMes = substr($_POST['date_occur'],4,2);
	$dateocDia = substr($_POST['date_occur'],6,2);
	$timeOccur = $dateocAno."-".$dateocMes."-".$dateocDia." ".$_POST['time_occur'].":00";
	
	} else {
	$timeOccur = $timeCreation;
	}
	
	$QAttrib = "SELECT * FROM administration_it WHERE refArea = '{$_POST['Area']}'";
	$Attrib = $GLOBALS['CONN']->db_array($QAttrib);
		
	$CSave = "INSERT INTO it_tickets (main_users_ref, main_domain_ref, main_office_ref, Description, ref_type, time_creation, priority, status, time_occur, refUserAtrib) 
	VALUES ('{$_SESSION['UtID']}','1','{$_SESSION['office']}','{$_POST['Description']}','{$_POST['Area']}','{$timeCreation}','1','1','{$timeOccur}','{$Attrib[1]['refUserAtrib']}')";
	$Save = $GLOBALS['CONN']->db_query($CSave);
	$Msg->SAVE_CLIENT = "Evento Criado.";
	} else {
	$Msg->SAVE_CLIENT = "Preencha todos os campos.";
	}
} 
?>
<script charset="utf-8" type="text/javascript" src="../../../Shared/Libraries/Objects/slide.js"></script>
<script charset="utf-8" type="text/javascript" src="../../../Shared/Libraries/JS/calendar.js"></script> 
<table width="100%"  border="0" cellpadding="10">
  <?php if (isset($_GET['action']) && $_GET['action'] == "show" || !isset($_GET['action'])) { ?>
<tr>
    <td>
	<table width="100%"  border="0" cellpadding="10" cellspacing="2">
      <tr bgcolor="#009999">
        <td>Suporte IT  | Eventos Resolvidos	      </td>
      </tr>
      <tr>
        <td rowspan="3" valign="top" bgcolor="#F2F2F2">	      <table width="100%" border="0" cellpadding="4" cellspacing="1">
		<tr bgcolor="#FFFFFF">
          <td width="10%">Ticket ID</td>
          <td width="20%">Aberto a:</td>
          <td width="20%">Por:</td>
          <td width="20%">Situação</td>
          <td width="30%">A/C</td>
          <td width="10%">Estado</td>
        </tr>
		  <?php 
				$CTickets = "SELECT  
							it_tickets.ref,
							it_tickets.main_users_ref AS UsersRef,
							 it_tickets.time_creation,
							  it_tickets.Description,
							   it_tickets.refUserAtrib,
							    it_tickets.status,
								it_status.name AS Status,
								 main_users.ref AS MainUsersRef,
								 main_users.name AS UserName
								FROM it_tickets 
								INNER JOIN main_users ON main_users.ref = it_tickets.main_users_ref
								INNER JOIN it_status ON it_status.ref = it_tickets.status
								WHERE status != '3' OR status != '5'";
				$Tickets = $GLOBALS['CONN']->db_array($CTickets);
				for ($t = 1; $t <= $Tickets[0]; $t++) { ?>
        <tr bgcolor="#FFFFFF">
          <td><a href="edit.php?TicketID=<?php echo $Tickets[$t]['ref'];?>"><?php echo $Tickets[$t]['ref'];?></a></td>
          <td><?php echo $Tickets[$t]['time_creation'];?></td>
          <td><?php echo $Tickets[$t]['UserName'];?></td>
          <td><?php echo $Tickets[$t]['Description'];?></td>
          <td><?php
		  					$CAtribs = "SELECT ref,name
										FROM main_users
										WHERE ref = {$Tickets[$t]['refUserAtrib']}";
							$Atribs = $GLOBALS['CONN']->db_array($CAtribs);
		  					print $Atribs[1]['name'];?></td>
          <td><?php echo $Tickets[$t]['Status'];?></td>
        </tr>
	    <?php } ?>
          </table></td>
        </tr>
    </table>
	</td>
  </tr>
  <?php } elseif (isset($_GET['action']) && $_GET['action'] == "showhistory" || !isset($_GET['action'])) { ?>
<tr>
    <td>
	<table width="100%"  border="0" cellpadding="10" cellspacing="2">
      <tr bgcolor="#009999">
        <td>Suporte IT  | Eventos a aguardar Resolução </td>
      </tr>
      <tr>
        <td rowspan="3" valign="top" bgcolor="#F2F2F2">	      <table width="100%" border="0" cellpadding="4" cellspacing="1">
		<tr bgcolor="#FFFFFF">
          <td width="10%">Ticket ID</td>
          <td width="20%">Aberto a:</td>
          <td width="20%">Por:</td>
          <td width="20%">Situação</td>
          <td width="30%">A/C</td>
          <td width="10%">Estado</td>
        </tr>
		  <?php 
				$CTickets = "SELECT  
							it_tickets.ref,
							it_tickets.main_users_ref AS UsersRef,
							 it_tickets.time_creation,
							  it_tickets.Description,
							   it_tickets.refUserAtrib,
							    it_tickets.status,
								it_status.name AS Status,
								 main_users.ref AS MainUsersRef,
								 main_users.name AS UserName
								FROM it_tickets 
								INNER JOIN main_users ON main_users.ref = it_tickets.main_users_ref
								INNER JOIN it_status ON it_status.ref = it_tickets.status
								WHERE it_tickets.status = '3' OR it_tickets.status = '5'";
				$Tickets = $GLOBALS['CONN']->db_array($CTickets);
				for ($t = 1; $t <= $Tickets[0]; $t++) { ?>
        <tr bgcolor="#FFFFFF">
          <td><a href="edit.php?TicketID=<?php echo $Tickets[$t]['ref'];?>"><?php echo $Tickets[$t]['ref'];?></a></td>
          <td><?php echo $Tickets[$t]['time_creation'];?></td>
          <td><?php echo $Tickets[$t]['UserName'];?></td>
          <td><?php echo $Tickets[$t]['Description'];?></td>
          <td><?php
		  					$CAtribs = "SELECT ref, name
										FROM main_users
										WHERE ref = '{$Tickets[$t]['refUserAtrib']}'";
							$Atribs = $GLOBALS['CONN']->db_array($CAtribs);
		  					print $Atribs[1]['name'];?></td>
          <td><?php echo $Tickets[$t]['Status'];?></td>
        </tr>
	    <?php } ?>
          </table></td>
        </tr>
    </table>
	</td>
  </tr>
  <?php } elseif (isset($_GET['action']) && $_GET['action'] == "new") { ?>
  <tr>
    <td>
	<table width="100%"  border="0" cellpadding="10" cellspacing="2">
	<form action="index.php?action=new" method="post">
      <tr bgcolor="#009999">
        <td colspan="3">Suporte IT  | Abrir Evento </td>
      </tr>
	  <?php if(!isset($print_Var)) { ?> 
      <tr>
        <td rowspan="3" valign="top" bgcolor="#F2F2F2">
          <p><b>&lt;&gt;</b></p>
          <p>
            <?php if (isset($Msg->SAVE_CLIENT)) {print $Msg->SAVE_CLIENT;} ?>
      Aberto por:
      <?php echo $_SESSION['UtAD'];?>
          </p>
          <p>A:
              <?php echo date("Y-m-d H:i:s");?>
          </p>
          <p><strong>Data da ocorrência</strong>:<br>
              <input name="date_occur" type="text" id="date_occur2"
		  onFocus="this.select();lcs(this)" 
		  onClick="event.cancelBubble=true;this.select();lcs(this)" 
		  value="<?php if (isset($_POST['date_occur'])) {
		  print $_POST['date_occur']; } else { print "AAAAMMDD";
		  } ?>" size="10" />
              <input name="time_occur" type="text" id="time_occur2" value="00:00" size="2" maxlength="5">
          </p>
          <p>(apenas se diferente) </p></td>
        <td colspan="2" bgcolor="#F2F2F2">
          <table width="100%" border="0" cellpadding="10" cellspacing="1">
            <tr bgcolor="#FFFFFF">
              <td width="10%" rowspan="2" valign="top">Tipificação</td>
              <td>área </td>
              <td><select name="Area" onclick="javascript:window.refresh();">
                  <?php 
				$CType = "SELECT * FROM it_ticket_types WHERE level = '0'";

				$Type = $GLOBALS['CONN']->db_array($CType);
				for ($t = 1; $t <= $Type[0]; $t++) { ?>
                  <option value = "<?php echo $Type[$t]['ref'];?>">
                  <?php echo $Type[$t]['name'];?>
                  </option>
                  <?php } ?>
              </select></td>
            </tr>
            <tr>
              <td width="10%" bgcolor="#FFFFFF">Sub-área</td>
              <td width="70%" bgcolor="#FFFFFF"><p>
                  <select name="SubArea">
                    <?php 
				$CType = "SELECT * FROM it_ticket_types WHERE level = '{$_POST['Area']}'";
				$Type = $GLOBALS['CONN']->db_array($CType);
				for ($t = 1; $t <= $Type[0]; $t++) { ?>
                    <option value = "<?php echo $Type[$t]['ref'];?>">
                    <?php echo $Type[$t]['name'];?>
                    </option>
                    <?php } ?>
                  </select>
              </p></td>
            </tr>
            <tr bgcolor="#FFFFFF">
              <td valign="top">Descrição</td>
              <td colspan="2" valign="top"><p>
                  <textarea name="Description" cols="80" rows="8" id="Description"></textarea>
              </p></td>
            </tr>
            <tr>
              <td valign="top">&nbsp;</td>
              <td colspan="2" valign="top"><input name="SaveTicket" type="submit" id="SaveTicket" value="Salvar"></td>
            </tr>
          </table>
          <?php } ?>
        </td>
      </tr>
	  </form>
    </table>
	</td>
  </tr>
  <?php } ?> 
</table>
<?php print footer(); ?>